package ysq.xyg.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.model.Member;
import ysq.xyg.model.info.Page;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.BaseUserService;
import ysq.xyg.service.MemberService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;
import ysq.xyg.util.DateUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:15
 **/
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(MemberController.class);

    @Resource
    private MemberService memberService;
    @Resource
    private BaseUserService baseUserService;

    private Member member;

    @RequestMapping("/listPage")
    public ModelAndView List(HttpServletRequest request, HttpServletResponse response,String menuid){
        return new ModelAndView("/memberList");
    }

    @RequestMapping("/listSelectPage")
    public ModelAndView listSelectPage(HttpServletRequest request, HttpServletResponse response,String memberIds){
        return new ModelAndView("/memberSelectList").addObject("memberIds",memberIds);
    }

    @RequestMapping(value = "/JsonList",method = RequestMethod.POST)
    public @ResponseBody JSONObject JsonList(Member member, Page page){
        PageInfo<Member> pageInfo=memberService.pageInfo(member,page.getLimit(),page.getPage());
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",pageInfo.getTotal());
        if(pageInfo !=null && pageInfo.getList() !=null && pageInfo.getList().size() > 0){
            JSONArray rows = new JSONArray();
            for(Member item:pageInfo.getList()){
                item.setNickName(BaseUtil.decodeBase64(item.getNickName()));
            }
            jsonObject.put("data",pageInfo.getList());
        }
        return jsonObject;
    }



    @RequestMapping(value = "/selectEntity",method = RequestMethod.GET)
    public ModelAndView SelectEntity(String id){
        ModelAndView view=new ModelAndView("/memberUpdateOrSave");
        member=memberService.selectByPrimaryKey(id);
        view.addObject("member",member);
        return view;
    }

    @RequestMapping(value = "/JsonSelectEntity")
    public @ResponseBody JSONObject JsonList(String id) throws Exception {
        JSONObject jsonObject=new JSONObject();
        ReturnInfo returnInfo=new ReturnInfo();
        member=memberService.selectByPrimaryKey(id);
        if(member==null){
            returnInfo=new ReturnInfo(BaseConstant.FAIL,"会员ID不存在");
        }else{
            if(StringUtil.isNotEmpty(member.getNickName())){
                member.setNickName(BaseUtil.decodeBase64(member.getNickName()));
            }
            returnInfo.setResult(member);
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }

    @RequestMapping(value = "/selectByCardNo")
    public @ResponseBody JSONObject selectByCardNo(String cardNo) throws Exception {
        JSONObject jsonObject=new JSONObject();
        ReturnInfo returnInfo=new ReturnInfo();
        Member record = new Member();
        record.setCardNo(cardNo);
        List<Member> memberList = memberService.select(record);
        if (memberList == null || memberList.size() == 0) {
            returnInfo=new ReturnInfo(BaseConstant.FAIL,"会员ID不存在");
        } else {
            member = memberList.get(0);
            if(StringUtil.isNotEmpty(member.getNickName())){
                member.setNickName(BaseUtil.decodeBase64(member.getNickName()));
            }
            returnInfo.setResult(member);
        }
        jsonObject.put("returnInfo",returnInfo);
        return jsonObject;
    }




    @RequestMapping(value = "/updateGradeEntity",method = RequestMethod.POST)
    public @ResponseBody JSONObject insertUserEntity(String memberId, String grade) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONObject jsonObject = new JSONObject();
        // 判断是否已设置
        member = memberService.selectByPrimaryKey(memberId);
        member.setGrade(grade);
        int id = memberService.updateByPrimaryKeySelective(member);
        if(id <= 0){
            returnInfo = new ReturnInfo(BaseConstant.FAIL, BaseConstant.DATABASE_ERROR_INSERT);
        }
        jsonObject.put("returnInfo", returnInfo);
        return jsonObject;
    }


}
